1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#![allow(non_camel_case_types, non_snake_case)]

use crate::co;
use crate::decl::*;
use crate::kernel::ffi_types::*;
use crate::ole::privs::*;
use crate::prelude::*;
use crate::vt::*;

/// [`IDXGIAdapter`](crate::IDXGIAdapter) virtual table.
#[repr(C)]
pub struct IDXGIOutputVT {
	pub IDXGIObjectVT: IDXGIObjectVT,
	pub GetDesc: fn(COMPTR, PVOID) -> HRES,
	pub GetDisplayModeList: fn(COMPTR, u32, u32, *mut u32, PVOID) -> HRES,
	pub FindClosestMatchingMode: fn(COMPTR, PCVOID, PVOID, COMPTR) -> HRES,
	pub WaitForVBlank: fn(COMPTR) -> HRES,
	pub TakeOwnership: fn(COMPTR, COMPTR, BOOL) -> HRES,
	pub ReleaseOwnership: fn(COMPTR),
	pub GetGammaControlCapabilities: fn(COMPTR, PVOID) -> HRES,
	pub SetGammaControl: fn(COMPTR, PCVOID) -> HRES,
	pub GetGammaControl: fn(COMPTR, PVOID) -> HRES,
	pub SetDisplaySurface: fn(COMPTR, COMPTR) -> HRES,
	pub GetDisplaySurfaceData: fn(COMPTR, COMPTR) -> HRES,
	pub GetFrameStatistics: fn(COMPTR, PVOID) -> HRES,
}

com_interface! { IDXGIOutput: "ae02eedb-c735-4690-8d52-5a8dc20213aa";
	/// [`IDXGIOutput`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nn-dxgi-idxgioutput)
	/// COM interface over [`IDXGIOutputVT`](crate::vt::IDXGIOutputVT).
	///
	/// Automatically calls
	/// [`Release`](https://learn.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-release)
	/// when the object goes out of scope.
}

impl dxgi_IDXGIObject for IDXGIOutput {}
impl dxgi_IDXGIOutput for IDXGIOutput {}

/// This trait is enabled with the `dxgi` feature, and provides methods for
/// [`IDXGIOutput`](crate::IDXGIOutput).
///
/// Prefer importing this trait through the prelude:
///
/// ```no_run
/// use winsafe::prelude::*;
/// ```
pub trait dxgi_IDXGIOutput: dxgi_IDXGIObject {
	/// [`IDXGIOutput::FindClosestMatchingMode`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-findclosestmatchingmode)
	/// method.
	#[must_use]
	fn FindClosestMatchingMode(&self,
		mode_to_match: &DXGI_MODE_DESC,
		device_interface: Option<&impl ole_IUnknown>,
	) -> HrResult<DXGI_MODE_DESC>
	{
		let mut closest_match = DXGI_MODE_DESC::default();
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGIOutputVT>(self).FindClosestMatchingMode)(
					self.ptr(),
					mode_to_match as *const _ as _,
					&mut closest_match as *mut _ as _,
					device_interface.map_or(std::ptr::null_mut(), |p| p.ptr()),
				)
			},
		).map(|_| closest_match)
	}

	/// [`IDXGIOutput::GetDesc`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-getdesc)
	/// method.
	#[must_use]
	fn GetDesc(&self) -> HrResult<DXGI_OUTPUT_DESC> {
		let mut desc = DXGI_OUTPUT_DESC::default();
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGIOutputVT>(self).GetDesc)(
					self.ptr(),
					&mut desc as *mut _ as _,
				)
			},
		).map(|_| desc)
	}

	/// [`IDXGIOutput::GetDisplayModeList`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-getdisplaymodelist)
	/// method.
	#[must_use]
	fn GetDisplayModeList(&self,
		format: co::DXGI_FORMAT,
		flags: co::DXGI_ENUM_MODES,
	) -> HrResult<Vec<DXGI_MODE_DESC>>
	{
		let mut num_modes = u32::default();
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGIOutputVT>(self).GetDisplayModeList)(
					self.ptr(),
					format.raw(),
					flags.raw(),
					&mut num_modes,
					std::ptr::null_mut(),
				)
			},
		)?;

		let mut modes = vec![DXGI_MODE_DESC::default(); num_modes as _];
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGIOutputVT>(self).GetDisplayModeList)(
					self.ptr(),
					format.raw(),
					flags.raw(),
					&mut num_modes,
					modes.as_mut_ptr() as _,
				)
			},
		).map(|_| modes)
	}

	/// [`IDXGIOutput::GetDisplaySurfaceData`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-getdisplaysurfacedata)
	/// method.
	fn GetDisplaySurfaceData(&self,
		destination: &impl dxgi_IDXGISurface,
	) -> HrResult<()> {
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGIOutputVT>(self).GetDisplaySurfaceData)(
					self.ptr(),
					destination.ptr(),
				)
			},
		)
	}

	/// [`IDXGIOutput::GetFrameStatistics`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-getframestatistics)
	/// method.
	#[must_use]
	fn GetFrameStatistics(&self) -> HrResult<DXGI_FRAME_STATISTICS> {
		let mut stats = DXGI_FRAME_STATISTICS::default();
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGIOutputVT>(self).GetFrameStatistics)(
					self.ptr(),
					&mut stats as *mut _ as _,
				)
			},
		).map(|_| stats)
	}

	/// [`IDXGIOutput::GetGammaControl`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-getgammacontrol)
	/// method.
	#[must_use]
	fn GetGammaControl(&self) -> HrResult<DXGI_GAMMA_CONTROL> {
		let mut array = DXGI_GAMMA_CONTROL::default();
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGIOutputVT>(self).GetGammaControl)(
					self.ptr(),
					&mut array as *mut _ as _,
				)
			},
		).map(|_| array)
	}

	/// [`IDXGIOutput::GetGammaControlCapabilities`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-getgammacontrolcapabilities)
	/// method.
	#[must_use]
	fn GetGammaControlCapabilities(&self,
	) -> HrResult<DXGI_GAMMA_CONTROL_CAPABILITIES>
	{
		let mut capa = DXGI_GAMMA_CONTROL_CAPABILITIES::default();
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGIOutputVT>(self).GetGammaControlCapabilities)(
					self.ptr(),
					&mut capa as *mut _ as _,
				)
			},
		).map(|_| capa)
	}

	/// [`IDXGIOutput::ReleaseOwnership`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-releaseownership)
	/// method.
	fn ReleaseOwnership(&self) {
		unsafe { (vt::<IDXGIOutputVT>(self).ReleaseOwnership)(self.ptr()) };
	}

	/// [`IDXGIOutput::SetDisplaySurface`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-setdisplaysurface)
	/// method.
	fn SetDisplaySurface(&self,
		scanout_surface: &impl dxgi_IDXGISurface,
	) -> HrResult<()>
	{
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGIOutputVT>(self).SetDisplaySurface)(
					self.ptr(),
					scanout_surface.ptr(),
				)
			},
		)
	}

	/// [`IDXGIOutput::SetGammaControl`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-setgammacontrol)
	/// method.
	fn SetGammaControl(&self, array: &DXGI_GAMMA_CONTROL) -> HrResult<()> {
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGIOutputVT>(self).SetGammaControl)(
					self.ptr(),
					array as *const _ as _,
				)
			},
		)
	}

	/// [`IDXGIOutput::TakeOwnership`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-takeownership)
	/// method.
	fn TakeOwnership(&self,
		device: &impl ole_IUnknown,
		exclusive: bool,
	) -> HrResult<()>
	{
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGIOutputVT>(self).TakeOwnership)(
					self.ptr(),
					device.ptr(),
					exclusive as _,
				)
			},
		)
	}

	fn_com_noparm! { WaitForVBlank: IDXGIOutputVT;
		/// [`IDXGIOutput::WaitForVBlank`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-waitforvblank)
		/// method.
	}
}